//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package test.classify.面向对象案例.com.gq.ui;

import java.awt.Component;
import java.awt.LayoutManager;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.border.BevelBorder;

public class GameJFrame extends JFrame implements KeyListener {
    int[][] win = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 0}};
    boolean winFlag = true;
    int[][] date = new int[4][4];
    int x = 0;
    int y = 0;
    int count = 0;
    String pictureType = "animal";
    String path = "";

    public GameJFrame() {
        this.initJFrame();
        this.initJMenuBar();
        this.randomPicturePath();
        this.initImageNumber();
        this.initAddImageJLabel();
        this.setVisible(true);
    }

    private void initJFrame() {
        this.setSize(608, 680);
        this.setTitle("拼图游戏 1.0");
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo((Component)null);
        this.setDefaultCloseOperation(3);
        this.setLayout((LayoutManager)null);
        this.addKeyListener(this);
    }

    private void initJMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        JMenu fuctionMenu = new JMenu("功能");
        JMenuItem replayItem = new JMenuItem("重新游戏");
        fuctionMenu.add(replayItem);
        JMenuItem reLoginItem = new JMenuItem("重新登录");
        fuctionMenu.add(reLoginItem);
        JMenuItem closeItem = new JMenuItem("关闭游戏");
        fuctionMenu.add(closeItem);
        JMenu aboutMenu = new JMenu("关于我们");
        JMenuItem wxItem = new JMenuItem("关于我们");
        aboutMenu.add(wxItem);
        menuBar.add(fuctionMenu);
        menuBar.add(aboutMenu);
        this.setJMenuBar(menuBar);
    }

    private void initImageNumber() {
        int[] arr = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

        for(int i = 0; i < arr.length; ++i) {
            Random random = new Random();
            int index = random.nextInt(arr.length);
            int temp = arr[index];
            arr[index] = arr[i];
            arr[i] = temp;
        }

        for(int i = 0; i < 4; ++i) {
            System.arraycopy(arr, i * 4, this.date[i], 0, 4);
        }

    }

    private void initAddImageJLabel() {
        this.getContentPane().removeAll();
        this.isWin();

        for(int i = 0; i < 4; ++i) {
            for(int j = 0; j < 4; ++j) {
                if (this.date[i][j] == 0) {
                    this.x = i;
                    this.y = j;
                }

                JLabel jLabel = new JLabel(new ImageIcon(this.path + this.date[i][j] + ".jpg"));
                jLabel.setBounds(105 * j + 83, 105 * i + 134, 105, 105);
                jLabel.setBorder(new BevelBorder(1));
                this.getContentPane().add(jLabel);
            }

            JLabel jLabel = new JLabel("总步数：" + this.count);
            jLabel.setBounds(20, 20, 100, 20);
            this.getContentPane().add(jLabel);
            this.getContentPane().repaint();
        }

        JLabel background = new JLabel(new ImageIcon("src\\test\\classify\\面向对象案例\\com\\gq\\image\\background.png"));
        background.setBounds(40, 40, 508, 560);
        this.getContentPane().add(background);
    }

    public void keyTyped(KeyEvent e) {
    }

    public void keyPressed(KeyEvent e) {
        if (!this.winFlag) {
            int code = e.getKeyCode();
            if (code == 65) {
                this.getContentPane().removeAll();
                JLabel jLabel = new JLabel(new ImageIcon(this.path + "all.jpg"));
                jLabel.setBounds(83, 134, 420, 420);
                this.getContentPane().add(jLabel);
                jLabel = new JLabel("总步数：" + this.count);
                jLabel.setBounds(20, 20, 100, 20);
                this.getContentPane().add(jLabel);
                jLabel = new JLabel(new ImageIcon("src\\test\\classify\\面向对象案例\\com\\gq\\image\\background.png"));
                jLabel.setBounds(40, 40, 508, 560);
                this.getContentPane().add(jLabel);
                this.getContentPane().repaint();
            }

        }
    }

    public void keyReleased(KeyEvent e) {
        if (!this.winFlag) {
            int code = e.getKeyCode();
            if (code == 37) {
                if (this.y >= 0 && this.y < 3) {
                    this.date[this.x][this.y] = this.date[this.x][++this.y];
                    this.date[this.x][this.y] = 0;
                    this.initAddImageJLabel();
                    ++this.count;
                }
            } else if (code == 38) {
                if (this.x >= 0 && this.x < 3) {
                    this.date[this.x][this.y] = this.date[++this.x][this.y];
                    this.date[this.x][this.y] = 0;
                    this.initAddImageJLabel();
                    ++this.count;
                }
            } else if (code == 39) {
                if (this.y > 0 && this.y < 4) {
                    this.date[this.x][this.y] = this.date[this.x][--this.y];
                    this.date[this.x][this.y] = 0;
                    this.initAddImageJLabel();
                    ++this.count;
                }
            } else if (code == 40) {
                if (this.x > 0 && this.x < 4) {
                    this.date[this.x][this.y] = this.date[--this.x][this.y];
                    this.date[this.x][this.y] = 0;
                    this.initAddImageJLabel();
                    ++this.count;
                }
            } else if (code == 65) {
                this.initAddImageJLabel();
            } else if (code == 87) {
                this.date = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 0}};
                this.initAddImageJLabel();
            }

        }
    }

    public void randomPicturePath() {
        Random random = new Random();
        String var10001 = this.pictureType;
        this.path = "src\\test\\classify\\面向对象案例\\com\\gq\\image\\" + var10001 + "\\" + this.pictureType + (random.nextInt(7) + 1) + "\\";
    }

    public void isWin() {
        this.winFlag = true;

        for(int i = 0; i < this.date.length; ++i) {
            for(int j = 0; j < this.date[i].length; ++j) {
                if (this.date[i][j] != this.win[i][j]) {
                    this.winFlag = false;
                    break;
                }
            }
        }

        if (this.winFlag) {
            JLabel background = new JLabel(new ImageIcon("src\\test\\classify\\面向对象案例\\com\\gq\\image\\win.png"));
            background.setBounds(203, 283, 197, 73);
            this.getContentPane().add(background);
        }

    }
}
